package com.salton123.soulove.sailfish.video

import android.net.Uri
import android.os.Build
import androidx.documentfile.provider.DocumentFile
import com.blankj.utilcode.util.FileUtils
import com.salton123.app.BaseApplication
import com.salton123.log.XLog
import com.salton123.soulove.sailfish.db.SailfishDatabase
import com.salton123.soulove.sailfish.db.VideoItem
import com.salton123.soulove.utils.Utils
import com.salton123.util.PreferencesUtils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

/**
 * Time:2021/9/10 3:08 下午
 * Author:
 * Description:
 */
object SailfishManager {
    private const val TAG = "SailfishManager"
    val coroutineScope = CoroutineScope(Dispatchers.IO)
    var toPlayVideoList: MutableList<VideoItem> = mutableListOf()
    var toBatchOperateList: MutableList<VideoItem> = mutableListOf()
    val androidDataUri = Uri.parse("content://com.android.externalstorage.documents/tree/primary%3AAndroid%2Fdata")
    val videoList = SailfishDatabase.Impl.videoDao().loadAll(false, "").value

    fun deleteVideo(videoItem: VideoItem) {
        var deleteSize = getDeleteSize() + videoItem.size
        PreferencesUtils.putLong("delete_size", deleteSize)
        coroutineScope.launch {
            SailfishDatabase.Impl.videoDao().delete(videoItem.filePath)
            if (Utils.isAboveAndroid11()) {
                val documentFile = DocumentFile.fromTreeUri(BaseApplication.sInstance, Uri.parse(videoItem.filePath))
                val ret = documentFile?.delete()
                XLog.i(TAG, "delete file $documentFile,ret:$ret")
            } else {
                FileUtils.delete(videoItem.filePath)
            }
        }
    }

    fun getDeleteSize(): Long {
        return PreferencesUtils.getLong("delete_size", 0)
    }

    fun getTotalSize(): Long {
        return PreferencesUtils.getLong("total_size", 0)
    }

    fun addTotalSize(size: Long) {
        var deleteSize = getTotalSize() + size
        PreferencesUtils.putLong("total_size", deleteSize)
    }
}